package edu.uoc.tfc.web.tags;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;



public class FormTag extends EmktBaseTag
{

	


	private static final long serialVersionUID = 1L;



	private static final Logger LOG = Logger.getLogger(FormTag.class);



	private FormType type;
	
	private String action;
	private String enctype;
	private String method;
	private String target;


	public final String getAction() {
		return action;
	}

	public final void setAction(String action) {
		this.action = action;
	}

	public final String getMethod() {
		return method;
	}

	public final void setMethod(String method) {
		this.method = method;
	}

	public final String getTarget() {
		return target;
	}

	public final void setTarget(String target) {
		this.target = target;
	}

	public final String getEnctype() {
		return enctype;
	}

	public final void setEnctype(String enctype) {
		this.enctype = enctype;
	}

	public FormType getType() {
	    return type;
    }

	public void setType(FormType type) {
	    this.type = type;
    }



	@Override
	public int doStartTag() throws JspException
	{
		try
		{
			JspWriter out = pageContext.getOut();
			write(out,"<br />");
			write(out,"<form class=\"form-horizontal\" id=\"" + getId() + "\"");
			
			if (StringUtils.isNotEmpty(getAction())) {
				write(out," action=\"" + getAction() + "\" ");
			}
			
			if (StringUtils.isNotEmpty(getEnctype())) {
				write(out," enctype=\"multipart/form-data\" ");
			}
			
			if (StringUtils.isNotEmpty(getMethod())) {
				write(out," method=\"post\" ");
			}
			
			if (StringUtils.isNotEmpty(getTarget())) {
				write(out," target=\"upload_target\" ");
			}
			
			write(out," >");
		}
		catch (Exception e)
		{
			String message = "Error generando Formulario: " + this.getProperty();
			LOG.error(message, e);
			throw new JspException(message);
		}

		return EVAL_BODY_INCLUDE;
	}



	@Override
	public int doEndTag() throws JspException
	{
		try
		{
			JspWriter out = pageContext.getOut();
			write(out,"</form>");
		}
		catch (Exception e)
		{
			String message = "Error finalizando Formulario: " + this.getProperty();
			LOG.error(message, e);
			throw new JspException(message);
		}

		return EVAL_PAGE;
	}



	@Override
    public boolean isReadonly()
	{
	    return this.type == FormType.Readonly;
    }



	
}
